# !/bin/bash

echo "----------------- 开始 --------------------"

set -e
BUILD_PATH="./build/archieve" # 包文件夹
SCHEME_NAME="shunguang"; # sheme名称
ARCHIEVE_PATH=".${BUILD_PATH}/iOS" #ipa包路径
EXPORT_PATH=".${BUILD_PATH}/iOS" #导出项目路径
ExportOptions_NAME="../script/ExportOptions.plist" #证书配置路径
WORKSPACE_NAME="./shunguang.xcworkspace"; #工作空间路径
PROJECT_NAME="./shunguang.xcodeproj"; #工作空间路径
IPA_PATH=".${BUILD_PATH}/iOS/${SCHEME_NAME}.ipa" # IPA 包路径
PWD=`pwd` # 当前目录
MSG=`git log -3 --date=format:'%Y-%m-%d %H:%M:%S' --pretty=format:"* %cd - %s "` # 最近 3 条更新


echo "----------------- iOS 打包开始 --------------------"
echo "清理文件夹-开始:${BUILD_PATH}"
rm -rf ${BUILD_PATH}
echo "清理文件夹-完毕:${BUILD_PATH}"
mkdir -p ${BUILD_PATH}

cd ios

# clean
xcodebuild clean \
-configuration release

# 打包   # -workspace ${WORKSPACE_NAME} \
xcodebuild archive \
-project ${PROJECT_NAME} \
-configuration release \
-scheme ${SCHEME_NAME} \
-archivePath ${ARCHIEVE_PATH} \
# >>"${BUILD_PATH}/archieve.log" 2>&1

# 导出
xcodebuild -exportArchive \
-archivePath "${ARCHIEVE_PATH}.xcarchive" \
-configuration release \
-exportPath ${EXPORT_PATH} \
-exportOptionsPlist ${ExportOptions_NAME} \
# >>"${BUILD_PATH}/ipa.log" 2>&1

cd ..

echo "----------------- iOS 打包结束 --------------------"

echo "----------------- 上传蒲公英开始 --------------------"

echo "----------------- 上传 iOS --------------------"

env=`cat /tmp/ENV.txt`

curl -F "file=@${PWD}/build/archieve/iOS/${SCHEME_NAME}.ipa" \
 -F "uKey=f700ce92efba61c69025108a1692959d" -F "_api_key=d643fccc60b8946ce543ef667562bebb" \
  -F "updateDescription=服务器环境:${env}\n${MSG}" https://qiniu-storage.pgyer.com/apiv1/app/upload > /tmp/upload_ios.txt

echo "----------------- 上传蒲公英结束 --------------------"


echo "----------------- 发送到钉钉开始 --------------------"

node script/ios.js

echo "----------------- 发送到钉钉结束 --------------------"

echo "----------------- 结束 --------------------"